package com.rcpcompany.examples.flows.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

import com.rcpcompany.examples.flows.directors.FirstDirector;
import com.rcpcompany.ui.flows.IFlowContainer;
import com.rcpcompany.ui.flows.IFlowContainerListener;
import com.rcpcompany.ui.flows.IFlowDirector;
import com.rcpcompany.utils.logging.LogUtils;

public class Selector extends ViewPart implements IFlowContainerListener {

	public Selector() {
		myFirstDirector = new FirstDirector("First");
		mySecondDirector = new FirstDirector("Second");
	}

	final protected IFlowDirector myFirstDirector;
	final protected IFlowDirector mySecondDirector;

	@Override
	public void createPartControl(Composite parent) {
		final Composite composite = new Composite(parent, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);

		final Label firstFlowLabel = new Label(composite, SWT.NONE);
		firstFlowLabel.setText("First flow:");

		final Button button = new Button(composite, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				startDirector(myFirstDirector);
			}
		});
		button.setText("Start");

		final Label secondFlowLabel = new Label(composite, SWT.NONE);
		secondFlowLabel.setText("Second Flow:");

		final Button startButton = new Button(composite, SWT.NONE);
		startButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				startDirector(mySecondDirector);
			}
		});
		startButton.setText("Start");
	}

	protected void startDirector(IFlowDirector director) {
		getFlowContainer().openDirector(director);
	}

	protected IFlowContainer myFlowContainer;

	public IFlowContainer getFlowContainer() {
		if (myFlowContainer == null) {
			getSite().getPage().getWorkbenchWindow().getShell().getDisplay().syncExec(new Runnable() {
				public void run() {
					try {
						myFlowContainer = (IFlowContainer) getSite().getPage().showView(
								"com.rcpcompany.examples.flows.views.FlowView", null, IWorkbenchPage.VIEW_CREATE);
						myFlowContainer.addListener(Selector.this);
					} catch (PartInitException ex) {
						LogUtils.error(this, ex);
					}
				}
			});
		}

		return myFlowContainer;
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	@Override
	public void directorActivated(IFlowDirector director) {
	}

	@Override
	public void directorAllClosed(boolean done) {
	}

	@Override
	public void directorClosed(IFlowDirector director) {
		System.out.println(director.getTitle() + " finished");
	}

	@Override
	public void directorOpened(IFlowDirector director) {
	}

}
